package com.yunsc.order.sevice.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yunsc.common.exception.YunscException;
import com.yunsc.media.service.YunscUploadFileService;
import com.yunsc.order.dao.YunscOrderGoodsDao;
import com.yunsc.order.entity.YunscOrderEntity;
import com.yunsc.order.entity.YunscOrderGoodsEntity;
import com.yunsc.order.sevice.YunscOrderGoodsService;
import com.yunsc.order.vo.CheckOrderGoodsVO;
import com.yunsc.order.vo.GoodsRankingVO;
import com.yunsc.order.vo.OrderGoodsVO;
import com.yunsc.product.service.YunscGoodsService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("yunscOrderGoodsService")
public class YunscOrderGoodsServiceImpl extends ServiceImpl<YunscOrderGoodsDao, YunscOrderGoodsEntity> implements YunscOrderGoodsService {

    @Resource
    private YunscUploadFileService uploadFileService;
    @Resource
    private YunscGoodsService goodsService;
    @Resource
    private YunscOrderGoodsService orderGoodsService;

    @Override
    public OrderGoodsVO queryOrderGoodsById(Integer orderGoodsId) {
        YunscOrderGoodsEntity orderGoodsEntity = this.getById(orderGoodsId);
        OrderGoodsVO orderGoodsVO = new OrderGoodsVO();
        BeanUtils.copyProperties(orderGoodsEntity, orderGoodsVO);
        orderGoodsVO.setGoodsImage(uploadFileService.queryFileUrlById(orderGoodsEntity.getImageId()));
        return orderGoodsVO;

    }

    @Override
    public int queryUserOrderGoodsNum(Integer goodsId, Integer userId) {
        LambdaQueryWrapper<YunscOrderGoodsEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YunscOrderGoodsEntity::getGoodsId, goodsId)
               .eq(YunscOrderGoodsEntity::getUserId, userId);
        List<YunscOrderGoodsEntity> list = this.list(lqw);
        if (CollectionUtils.isEmpty(list)){
            return 0;
        }
        return list.stream().mapToInt(YunscOrderGoodsEntity::getTotalNum).sum();

    }

    @Override
    public void saveOrderGoodsDb(Integer userId, YunscOrderEntity orderEntity, List<CheckOrderGoodsVO> goodsList, Integer storeId) {
        if (CollectionUtils.isEmpty(goodsList)) {
            throw new YunscException("创建订单异常，商品不存在");
        }
        List<YunscOrderGoodsEntity> orderGoodsList = new ArrayList<>();
        for (CheckOrderGoodsVO goods : goodsList) {
            YunscOrderGoodsEntity orderGoodsEntity = new YunscOrderGoodsEntity();
            BeanUtils.copyProperties(goods, orderGoodsEntity);
            Integer imageId = goods.getSkuInfo().getImageId();
            if (imageId == null || imageId == 0) {
                imageId = goodsService.queryGoodsDefaultImage(goods.getSkuInfo().getGoodsId()).getFileId();
            }
            orderGoodsEntity.setImageId(imageId);
            // List<Map<String, Object>> goodsProps = goods.getSkuInfo().getGoodsProps();
            orderGoodsEntity.setOrderId(orderEntity.getOrderId());
            Object goodsProps = goods.getSkuInfo().getGoodsProps();
            orderGoodsEntity.setGoodsProps(JSON.toJSONString(goodsProps));

            orderGoodsEntity.setLinePrice(goods.getSkuInfo().getLinePrice());
            orderGoodsEntity.setGoodsWeight(goods.getSkuInfo().getGoodsWeight());
            orderGoodsEntity.setGradeRatio(Integer.valueOf(goods.getGradeRatio()));
            orderGoodsEntity.setCouponMoney(String.valueOf(goods.getCouponMoney()));
            orderGoodsEntity.setPointsBonus(goods.getPointsBonus());
            orderGoodsEntity.setIsUserGrade(goods.getIsUserGrade() ? 1 : 0);
            orderGoodsEntity.setUserId(userId);
            orderGoodsEntity.setStoreId(storeId);
            orderGoodsEntity.setContent("");
            orderGoodsList.add(orderGoodsEntity);
        }
        // 订单商品保存
        boolean update = orderGoodsService.saveBatch(orderGoodsList);
        if (!update) {
            throw new YunscException("创建商品订单异常");
        }
    }

    @Override
    public List<YunscOrderGoodsEntity> queryOrderGoodsListByOrderId(Integer orderId) {
        LambdaQueryWrapper<YunscOrderGoodsEntity> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YunscOrderGoodsEntity::getOrderId, orderId);
        return this.list(lqw);

    }

    @Override
    public List<GoodsRankingVO> queryGoodsRanking(Integer storeId) {
        return this.baseMapper.queryGoodsRanking(storeId);
    }


}